package com.itheima.api;

import com.itheima.domain.Comment;
import com.itheima.domain.Movement;
import com.itheima.enums.CommentType;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.Constants;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import javax.annotation.Resource;
import java.util.List;

@DubboService
public class CommentsApiImpl implements CommentsApi{

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 根据动态id查询出评论列表
     * @param movementId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult comments(String movementId, Integer page, Integer pagesize,CommentType commentType) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(CommentType.COMMENT.getType()))
                .skip((long) (page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        long count = mongoTemplate.count(query,Comment.class);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return new PageResult(page,pagesize, (int) count,comments);
    }

    /**
     * 发布评论,获取评论数量
     * @param comment
     */
    @Override
    public Integer publishComments(Comment comment) {
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (movement != null){
            //设置被评论人id
            comment.setPublishUserId(movement.getUserId());
        }
        mongoTemplate.save(comment);
        //动态表中也有关联字段，也需要设置
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.COMMENT.getType()){
            //发布的是评论
            update.inc("commentCount",1);
        }else if (comment.getCommentType() == CommentType.LIKE.getType()){
            //点赞
            update.inc("likeCount",1);
        }else if (comment.getCommentType() == CommentType.LOVE.getType()){
            //喜欢
            update.inc("loveCount",1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return modify.statisCount(comment.getCommentType());
    }

    /**
     * 判断是否已经点赞
     * @param userId
     * @param movementId
     * @param like
     * @return
     */
    @Override
    public Boolean hasLike(Long userId, String movementId, CommentType like) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("userId")
                .is(userId).and("commentType").is(like.getType()));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        return comment != null;
    }

    /**
     * 取消点赞
     * @param comment
     * @return
     */
    @Override
    public Integer delete(Comment comment) {
        //删除comment表中数据
        Query query = Query.query(Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(query,Comment.class);
        //修改动态表中数据
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if(comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(comment.getCommentType() );
    }


}
